package com.zzy.demo.web;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zzy.demo.utils.UUIDGenerator;


@Controller
public class UeditorController {
	
	
	@RequestMapping(value = "/upload")
	@ResponseBody
	public  Map<String, String> imgUpload(MultipartFile upfile,HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String, String> result = new HashMap<String, String>();
		
		if(null==upfile){
			return result;
		}
		String fileName=createFileName(upfile.getOriginalFilename());
		String filePath=request.getSession().getServletContext().getRealPath("/upload");
		
		String httpPath=request.getContextPath()+"/upload/"+fileName;
		try {
			byte[] imgByte =readStream(upfile.getInputStream());
			
			File file=byte2File(imgByte,filePath,fileName);
			
			result.put("url", httpPath);
			result.put("size", String.valueOf(file.length()));
			result.put("type", file.getName().substring(file.getName().lastIndexOf(".")));
			result.put("state", "SUCCESS");
			System.out.println(httpPath+"----------"+filePath);
			return result;
			
		} catch (IOException e) {
			System.out.println("富文本框图片上传错误");
		    e.printStackTrace();
		}

		return result;


	
	}


	
	
	/**
	 * 得到图片字节流 数组大小
	 */
	public static String createFileName(String originalFileName) throws Exception {
		String fileName=UUIDGenerator.nextId()+"-"+originalFileName;
		return fileName;
	}
	
	

	/**
	 * 得到图片字节流 数组大小
	 */
	public static byte[] readStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = -1;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		outStream.close();
		inStream.close();
		return outStream.toByteArray();
	}

	/**
	 * byte[]转file
	 * 
	 * @Title: title
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param buf
	 * @param filePath
	 * @param fileName
	 * @version V2.0
	 * @author guokaige
	 * @Date 2017-2-7 下午2:06:32
	 */
	public File byte2File(byte[] buf, String filePath, String fileName) {
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		try {
			File dir = new File(filePath);
			if (!dir.exists() && dir.isDirectory()) {
				boolean flag = dir.mkdirs();
				if (!flag) {
					System.out.println("创建父级目录失败！");
				}
			}
			file = new File(filePath + File.separator + fileName);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(buf);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}

}
